# -*- coding: utf-8 -*-
"""
样式管理器 - 统一管理所有UI组件的样式
"""
from PySide6.QtWidgets import QWidget, QVBoxLayout, QHBoxLayout
from PySide6.QtCore import Qt


class StyleManager:
    """统一样式管理器"""
    
    # 主题色彩配置
    COLORS = {
        'primary': '#0078d4',
        'primary_light': '#e6f3ff',  # 添加缺失的 primary_light 颜色
        'primary_hover': '#106ebe', 
        'primary_pressed': '#005a9e',
        'secondary': '#28a745',
        'secondary_hover': '#218838',
        'secondary_pressed': '#1e7e34',
        'danger': '#dc3545',
        'danger_hover': '#c82333',
        'danger_pressed': '#bd2130',
        'warning': '#ffc107',
        'info': '#17a2b8',
        'light': '#f8f9fa',
        'dark': '#343a40',
        'text_primary': '#333',
        'text_secondary': '#666',
        'text_muted': '#999',
        'border': '#dee2e6',
        'background': '#ffffff',
        'background_light': '#f0f8ff',
        'background_secondary': '#e8f5e8',
        'surface': '#f8f9fa',  # 添加 surface 颜色
        'hover': '#e9ecef'     # 添加 hover 颜色
    }
    
    # 字体大小配置
    FONT_SIZES = {
        'title': '24px',
        'subtitle': '20px',
        'large': '18px',
        'medium': '16px',
        'normal': '14px',
        'small': '12px'
    }
    
    # 字体家族配置
    FONT_FAMILY = {
        'default': 'Microsoft YaHei, sans-serif'
    }

    # 间距配置
    SPACING = {
        'xs': '5px',
        'sm': '10px',
        'md': '15px',
        'lg': '20px',
        'xl': '30px'
    }
    
    # 圆角配置
    BORDER_RADIUS = {
        'small': '4px',
        'medium': '6px',
        'large': '10px',
        'xl': '15px'
    }
    
    @classmethod
    def get_title_style(cls, color_theme='primary') -> str:
        """获取标题样式"""
        if color_theme == 'primary':
            bg_color = cls.COLORS['background_light']
            border_color = cls.COLORS['primary']
        elif color_theme == 'secondary':
            bg_color = cls.COLORS['background_secondary']
            border_color = cls.COLORS['secondary']
        else:
            bg_color = cls.COLORS['light']
            border_color = cls.COLORS['border']
            
        return f"""
            QLabel {{
                font-family: {cls.FONT_FAMILY['default']};
                font-size: {cls.FONT_SIZES['title']};
                font-weight: bold;
                color: {cls.COLORS['text_primary']};
                padding: {cls.SPACING['lg']};
                background-color: {bg_color};
                border-radius: {cls.BORDER_RADIUS['large']};
                border: 2px solid {border_color};
            }}
        """
    
    @classmethod
    def get_description_style(cls) -> str:
        """获取描述文本样式"""
        return f"""
            QLabel {{
                font-family: {cls.FONT_FAMILY['default']};
                font-size: {cls.FONT_SIZES['normal']};
                color: {cls.COLORS['text_secondary']};
                padding: {cls.SPACING['md']}; /* 增加填充 */
                line-height: 1.5;
                font-weight: normal; /* 添加字体粗细 */
                background-color: {cls.COLORS['background_light']}; /* 添加背景色 */
                border-radius: {cls.BORDER_RADIUS['small']}; /* 添加圆角 */
                border: 1px solid {cls.COLORS['border']}; /* 添加细边框 */
            }}
        """
    
    @classmethod
    def get_button_style(cls, color_theme='primary', size='medium') -> str:
        """获取按钮样式"""
        if color_theme == 'primary':
            bg_color = cls.COLORS['primary']
            hover_color = cls.COLORS['primary_hover']
            pressed_color = cls.COLORS['primary_pressed']
        elif color_theme == 'secondary':
            bg_color = cls.COLORS['secondary']
            hover_color = cls.COLORS['secondary_hover'] 
            pressed_color = cls.COLORS['secondary_pressed']
        elif color_theme == 'danger':
            bg_color = cls.COLORS['danger']
            hover_color = cls.COLORS['danger_hover']
            pressed_color = cls.COLORS['danger_pressed']
        else:
            bg_color = cls.COLORS['primary']
            hover_color = cls.COLORS['primary_hover']
            pressed_color = cls.COLORS['primary_pressed']
            
        if size == 'large':
            font_size = cls.FONT_SIZES['large']
            padding = f"{cls.SPACING['md']} {cls.SPACING['xl']}"
            min_width = "250px"
            min_height = "50px"
        elif size == 'small':
            font_size = cls.FONT_SIZES['small']
            padding = f"{cls.SPACING['xs']} {cls.SPACING['sm']}"
            min_width = "80px"
            min_height = "30px"
        else:  # medium
            font_size = cls.FONT_SIZES['medium']
            padding = f"{cls.SPACING['sm']} {cls.SPACING['lg']}"
            min_width = "200px"
            min_height = "40px"
            
        return f"""
            QPushButton {{
                font-family: {cls.FONT_FAMILY['default']};
                font-size: {font_size};
                font-weight: bold;
                color: white;
                background-color: {bg_color};
                border: none;
                padding: {padding};
                border-radius: {cls.BORDER_RADIUS['medium']};
                min-width: {min_width};
                min-height: {min_height};
            }}
            QPushButton:hover {{
                background-color: {hover_color};
            }}
            QPushButton:pressed {{
                background-color: {pressed_color};
            }}
        """
    
    @classmethod
    def get_outline_button_style(cls, color_theme='danger') -> str:
        """获取轮廓按钮样式"""
        if color_theme == 'danger':
            color = cls.COLORS['danger']
            hover_bg = cls.COLORS['danger']
        else:
            color = cls.COLORS['primary']
            hover_bg = cls.COLORS['primary']
            
        return f"""
            QPushButton {{
                font-family: {cls.FONT_FAMILY['default']};
                font-size: {cls.FONT_SIZES['normal']};
                color: {color};
                background-color: {cls.COLORS['background']};
                border: 1px solid {color};
                padding: {cls.SPACING['xs']} {cls.SPACING['md']};
                border-radius: {cls.BORDER_RADIUS['small']};
                min-width: 120px;
                min-height: 35px;
            }}
            QPushButton:hover {{
                background-color: {hover_bg};
                color: white;
            }}
        """
    
    @classmethod
    def get_text_edit_style(cls) -> str:
        """获取文本编辑区样式"""
        return f"""
            QTextEdit {{
                font-family: {cls.FONT_FAMILY['default']};
                font-size: {cls.FONT_SIZES['normal']};
                /* font-family: 'Courier New', monospace; */ /* 移除硬编码字体 */
                background-color: {cls.COLORS['light']};
                border: 1px solid {cls.COLORS['border']};
                border-radius: {cls.BORDER_RADIUS['small']};
                padding: {cls.SPACING['sm']};
            }}
        """
    
    @classmethod
    def get_message_box_style(cls, color_theme='primary') -> str:
        """获取消息框样式"""
        if color_theme == 'secondary':
            button_bg = cls.COLORS['secondary']
            button_hover = cls.COLORS['secondary_hover']
        else:
            button_bg = cls.COLORS['primary']
            button_hover = cls.COLORS['primary_hover']
            
        return f"""
            QMessageBox {{
                font-family: {cls.FONT_FAMILY['default']};
                font-size: {cls.FONT_SIZES['normal']};
                background-color: {cls.COLORS['background']};
            }}
            QMessageBox QLabel {{
                font-family: {cls.FONT_FAMILY['default']};
                color: {cls.COLORS['text_primary']};
                font-size: {cls.FONT_SIZES['medium']};
            }}
            QMessageBox QPushButton {{
                background-color: {button_bg};
                color: white;
                border: none;
                padding: {cls.SPACING['xs']} {cls.SPACING['md']};
                border-radius: {cls.BORDER_RADIUS['small']};
                font-family: {cls.FONT_FAMILY['default']};
                font-size: {cls.FONT_SIZES['normal']};
                min-width: 80px;
            }}
            QMessageBox QPushButton:hover {{
                background-color: {button_hover};
            }}
        """
    
    @classmethod
    def setup_layout(cls, layout, margins='medium', spacing='medium'):
        """设置布局的边距和间距"""
        # 边距设置
        if margins == 'none':
            margin_value = 0
        elif margins == 'xs':
            margin_value = 5
        elif margins == 'small':
            margin_value = 10
        elif margins == 'large':
            margin_value = 30
        elif margins == 'xl':
            margin_value = 40
        else:  # medium
            margin_value = 20
            
        # 间距设置
        if spacing == 'none':
            spacing_value = 0
        elif spacing == 'xs':
            spacing_value = 5
        elif spacing == 'small':
            spacing_value = 10
        elif spacing == 'large':
            spacing_value = 20
        elif spacing == 'xl':
            spacing_value = 30
        else:  # medium
            spacing_value = 15
            
        layout.setContentsMargins(margin_value, margin_value, margin_value, margin_value)
        layout.setSpacing(spacing_value)
    
    @classmethod
    def apply_title_style(cls, widget, text, color_theme='primary'):
        """应用标题样式到组件"""
        widget.setText(text)
        widget.setAlignment(Qt.AlignmentFlag.AlignCenter)
        widget.setStyleSheet(cls.get_title_style(color_theme))
    
    @classmethod
    def apply_description_style(cls, widget, text):
        """应用描述样式到组件"""
        widget.setText(text)
        widget.setAlignment(Qt.AlignmentFlag.AlignCenter)
        widget.setStyleSheet(cls.get_description_style())
    
    @classmethod
    def apply_button_style(cls, widget, text, color_theme='primary', size='medium'):
        """应用按钮样式到组件"""
        widget.setText(text)
        widget.setStyleSheet(cls.get_button_style(color_theme, size))
    
    @classmethod
    def apply_outline_button_style(cls, widget, text, color_theme='danger'):
        """应用轮廓按钮样式到组件"""
        widget.setText(text)
        widget.setStyleSheet(cls.get_outline_button_style(color_theme))
    
    @classmethod
    def apply_text_edit_style(cls, widget, placeholder_text=""):
        """应用文本编辑区样式到组件"""
        widget.setStyleSheet(cls.get_text_edit_style())
        if placeholder_text:
            widget.setPlainText(placeholder_text) 